
var date=echartUtil.GetDateStr(0);
var startDay=echartUtil.GetDateStr(-30);
var deptName="";

//相关业务逻辑
$(function(){
	page.setTime();
//	echart.getClock();
	page.bind();
//	page.getTotal(date,startDay);
	nameToIdUtil.topGetProductionDepartment("productionDepartment");
	echartUtil.getTopManager(deptName);
	page.getTableData(deptName,date,startDay);
	page.getEchart(deptName,date,startDay);
	$("#workShopName").text(deptName);
//	page.getReachParam();
});

var table;
var page={//页面总变量
		bind:function(){
			$("#isHidden").bind("click",function(){
				var text=$(this).text();
				if(text=="隐藏"){
					$(".middle").hide();
					$(this).text("显示");
				}else{
					$(".middle").show();
					$(this).text("隐藏");
				}
			});
			
			
			$("#downlodeExcle").bind("click",function(){
				var url="downlodeReachFile?workShop="+deptName+"&date="+date+"&startDay="+startDay;
				tableUtil.download(url);
			});
			
			//点击部门
			$("#totalTable").bind("change",function(){
				deptName=$(this).val();
				page.getTableData(deptName,date,startDay);
				page.getEchart(deptName,date,startDay);
				echartUtil.getTopManager(deptName);
			});
			
			$("#productionDepartment").bind("change",function(){
				var deptId=$("#productionDepartment").val();
				page.getTotal(deptId);
			});
		},
		deptBind:function(){
			//点击部门
			$(".deptClick").bind("click",function(){
				deptName=$(this).text();
				$("#workShopName").text(deptName);
				page.getTableData(deptName,date,startDay);
				page.getEchart(deptName,date,startDay);
				echartUtil.getManager(deptName);
			});
		},
		//获取所有车间达成率
		getTotal:function(deptId){
			nameToIdUtil.topGetWorkShopNameAndId("totalTable",deptId);
		},
		//获取表格数据
		getTableData:function(workShop,date,startDay){
			table=ajaxUtil.dataTablesAjax("dataTable",20,"getTableData",
					{'workShop':workShop,'date':date,'startDay':startDay},[
					{"data":"name"},
					{"data":"factoryId"},
					{"data":"procedur"},
					{"data":"capacity"},
					{"data":"oneTime"},
					{"data":"effectiveTime"},
					{"data":"workEffective",render: function (data) {
			            return data+'%';
			          }},
					{"data":"workDate"},
					{"data":"price"},
					{"data":"sumPrice"}
			]);
		},
		getEchart:function(workShop,date,startDay){
			ajaxUtil.ajaxRuquest('getPersonData',{ 'workShop':workShop,'date':date,'startDay':startDay},function(data){
				   var chartData=new Object();//渲染图表的数据
				   var chartName=new Array();//名字
				   var chartCapacity=new Array();//产能
				   var sumPrice=new Array();//单付时间
				   var chartEffectiveTime=new Array();//有效时间
				   var chartWorkEffective=new Array();//工作效率
				   for(var key in data){
					   console.log(data[key]);
					   chartName.push(data[key].name);
					   chartCapacity.push(data[key].capacity);
					   sumPrice.push(data[key].sumPrice);
					   chartEffectiveTime.push(data[key].effectiveTime);
					   chartWorkEffective.push(data[key].workEffective);
				   }
				   chartData.chartName=chartName;
				   chartData.chartCapacity=chartCapacity;
				   chartData.sumPrice=sumPrice;
				   chartData.chartWorkEffective=chartWorkEffective;
				   chartData.chartEffectiveTime=chartEffectiveTime;
				   echart.echarts(chartData);
			});
		},
		//设置本人页面参数（本页面查看部门部门）
		setReachParam:function(workShop){
			ajaxUtil.ajaxRuquest('pageParam/setReachParam',{'reachParam':workShop},function(data){
				console.log("员工达成率看板设置车间失败");
			});
		},
		//获取页面参数
		getReachParam:function(){
			$.ajax({
				url:'pageParam/getReachParam',
				success:function(data){
					if(data.result){
						if(deptName!=data.data.reachParam){
							deptName=data.data.reachParam;
							console.log(deptName);//
							page.getTableData(deptName);
						}
					}else{
						console.log("获取员工达成率看板车间失败");
					}
				}
			});
		},
       //设置时间
		setTime:function(){
			$("#startDay").val(startDay);
			$("#yestarday").val(date);
		}
}


var echart={
		echarts:function(chartData){
			//基于准备好的dom，初始化echarts实例
			var myChart = echarts.init(document.getElementById('chartDiv'));
			    myChart.setOption({
			        title: {
			            text: ''
			        },
			        toolbox: {
			            show: true,
			            orient: 'vertical',
			            left: 'right',
			            top: 'center',
			            feature: {
			                mark: {show: true},
			                dataView: {show: true, readOnly: false},
			                magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
			                restore: {show: true},
			                saveAsImage: {show: true}
			            }
			        },
			        tooltip: {},
			        legend: {
			            data:['作业产能','有效时间', '工作效率','总价']
			        },
			        xAxis: {
			            data: chartData.chartName
			        },
			        yAxis:  [
			        	{  
			                type : 'value' 
			            }  
			        ],
			        series:  [{
			            name: '作业产能',
			            type: 'bar',
			            data: chartData.chartCapacity,
			            itemStyle: {
		                    normal: {
		                        label: {
		                            show: true,
		                            positiong: 'top',
		                            formatter: '{c}'
		                        }
		                    }
		                }
			        },{
			            name: '有效时间',
			            type: 'bar',
			            data: chartData.chartEffectiveTime,
			            itemStyle: {
		                    normal: {
		                        label: {
		                            show: true,
		                            positiong: 'top',
		                            formatter: '{c} h'
		                        }
		                    }
		                }
			        },{
			            name: '工作效率',
			            type: 'bar',
			            data: chartData.chartWorkEffective,
			            itemStyle: {
		                    normal: {
		                        label: {
		                            show: true,
		                            positiong: 'top',
		                            formatter: '{c} %'
		                        }
		                    }
		                }
			        },{
			            name: '总价',
			            type: 'bar',
			            data: chartData.sumPrice,
			            itemStyle: {
		                    normal: {
		                        label: {
		                            show: true,
		                            positiong: 'top',
		                            formatter: '{c} ￥'
		                        }
		                    }
		                }
			        }]
			    });
		}
//		getClock:function(){
//			$.ajax({
//				url:'weiXin/getClock',
//				success:function(data){
//					if(data.result){
//						data=data.data;
//						var myChart = echarts.init(document.getElementById('clockChartDiv'));
//						 myChart.setOption({
//							 title: {
//						            text: '上下班打卡统计',
//						            x:'center',
//						            y: 'top'
//						        },
//							 tooltip: {
//							        trigger: 'item',
//							        formatter: "{a} <br/>{b}: {c} ({d}%)"
//							    },
//							    legend: {
//							    	orient: 'horizontal',
//							        x: 'center',
//							        y: 'bottom',
//							        data:['正常人数'+data.successUserCount,'异常人数'+data.errorUserCount]
//							    },
//							    series: [
//							        {
//							            name:'上班打卡',
//							            type:'pie',
//							            radius: ['50%', '60%'],
//							            avoidLabelOverlap: false,
//							            label: {
//							                normal: {
//							                    show: false,
//							                    position: 'center'
//							                },
//							                emphasis: {
//							                    show: true,
//							                    textStyle: {
//							                        fontSize: '30',
//							                        fontWeight: 'bold'
//							                    }
//							                }
//							            },
//							            labelLine: {
//							                normal: {
//							                    show: true
//							                }
//							            },
//							            data:[
//							                {value:data.successUserCount, name:'正常人数'+data.successUserCount},
//							                {value:data.errorUserCount, name:'异常人数'+data.errorUserCount}
//							            ]
//							        }
//							    ]
//						    });
//					}
//				}
//			});
//		}
}

//时间改变
function dataChange(){
	startDay=$("#startDay").val();
	date=$("#yestarday").val();
//	page.getTableData(deptName,date,startDay);
	page.getEchart(deptName,date,startDay);
	table.ajax.reload( null, false);
	page.getTotal(date,startDay);
}

setInterval ("dataChange()", 900000);//十五分钟更新一次数据
